package com.hongyuan.sellfitness.ui.home.activity;

import android.os.Bundle;
import androidx.lifecycle.ViewModelProviders;
import com.hongyuan.sellfitness.BR;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.AppViewModelFactory;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomActivity;
import com.hongyuan.sellfitness.data.md5.BaseUtil;
import com.hongyuan.sellfitness.databinding.ActivityBuyLessonBinding;
import com.hongyuan.sellfitness.ui.home.view_mode.BuyLessonViewModel;

public class BuyLessonActivity extends CustomActivity<ActivityBuyLessonBinding, BuyLessonViewModel> {

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_buy_lesson;
    }

    @Override
    public int initVariableId() {
        //注意了：这里是小写的viewModel
        return BR.viewModel;
    }

    @Override
    public BuyLessonViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用NetWorkViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(BuyLessonViewModel.class);
    }

    @Override
    public void initView() {
        super.initView();

        setOnRefresh(binding.refresh,REFRESH_0X4);
        binding.comBack.setOnClickListener(view -> finish());

        binding.classNum.addTextChangedListener(viewModel.watcher);
    }

    @Override
    public void initData() {
        super.initData();

        viewModel.clearParams().setParams("cp_id",getBundle().getString("cp_id"));
        viewModel.requestData(Constants.GET_COURSE_PRIVITE_INFO);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.uc.changeShowNum.observe(this, s -> {
            binding.classNum.setText(s.getShowNum());
            binding.classNum.setSelection(s.getStart());
        });
        viewModel.uc.showCouponName.observe(this,s -> {
            if(BaseUtil.isValue(s)){
                binding.couponName.setText(s);
            }else{
                binding.couponName.setText("请选择");
            }

        });
    }
}
